<?php

namespace app\teacher\controller\course;

use app\common\controller\Frontend;
use app\common\enums\StudentExperienceAppointmentOrderStatusEnum;
use app\common\model\banxi\student\StudentExperienceAppointment;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/*
 * 体验课预约
 * */
class ExperienceAppointment extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 分页-学生体验课程订单列表
     * */
    public function listExperienceAppointmentOrderPage()
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        $param['query']['EQ_orderStatus'] = StudentExperienceAppointmentOrderStatusEnum::PAID;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $StudentExperienceAppointmentModel = new StudentExperienceAppointment();
        $list = $StudentExperienceAppointmentModel->with(['studentInfo' => function ($query) {
            $query->field('id,name,phone');
            }])
            ->where($where)
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit'],
            ]);
        $this->ok('操作成功', result_row($list));
    }

    /*
     * 抢单
     * */
    public function catchOrder()
    {
        $param = $this->request->param(['id' => null]);

        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '参数错误'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $trainerId = $this->auth->id;
            $StudentExperienceAppointmentModel = new StudentExperienceAppointment();
            $StudentExperienceAppointmentModel->catchOrder($param['id'],  $trainerId);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功');
    }
}